/**
  ******************************************************************************
  * @file    ct75.c
  * @author  fanhui.meng
  * @version V1.0.0
  * @date    2019.9.20
  * @brief   
  ******************************************************************************
  */

#include "i2c.h"
#include "stdio.h"
#include "i2c_app.h"

/**************************pca9545**************************************/
void test_select_pca_channel(void)
{
	uint8_t channel_value_write = CHANNEL_0_MP5416;
	uint8_t channel_value_read = 0;
	
	if (HAL_I2C_Mem_Write(&hi2c2, (PCA9545_ADDR<<1)|0x00, 0x00, I2C_MEMADD_SIZE_8BIT,&channel_value_write,1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel write failed\n");
	}
	if (HAL_I2C_Mem_Read(&hi2c2, (PCA9545_ADDR<<1)|0x01, 0x00, I2C_MEMADD_SIZE_8BIT,&channel_value_read,1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel 2 failed\n");
	} else {
	  printf("\r\n Select_pca_channel %d \n",channel_value_read);
	}
	
}
void select_pca_channel(uint8_t channel)
{
	uint8_t channel_value = channel;
	
	/*if (HAL_I2C_Master_Transmit(&hi2c2, (PCA9545_ADDR<<1)|0x00, &channel_value, 1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel failed\n");
	}*/
	if (HAL_I2C_Mem_Write(&hi2c2, (PCA9545_ADDR<<1)|0x00, 0x00, I2C_MEMADD_SIZE_8BIT,&channel_value,1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel failed\n");
	}

}


/****************************************CT75*******************************************************************************/
void CT75_Alert_INIT(void)
{
	CT75_SET_ALERT_LIMIT(CT75_WRITE_ADDR1);
	CT75_SET_ALERT_LIMIT(CT75_WRITE_ADDR2);
	CT75_SET_ALERT_LIMIT(CT75_WRITE_ADDR3);
}

int16_t CT75_Read_Temperature(uint16_t ct75_addr)
{
	uint8_t CT75_Temperature_Data_Buffer[CT75_TEMPERATURE_NUMBER];
	int16_t ut;
    

	if(HAL_I2C_Mem_Read(&hi2c1, ct75_addr, CT75_CMD_ADDR, I2C_MEMADD_SIZE_8BIT,CT75_Temperature_Data_Buffer,CT75_TEMPERATURE_NUMBER, 1000) != HAL_OK)
	{
		CT75_TIMEOUT_UserCallback();
	}

	ut = (int16_t)(((((uint16_t)CT75_Temperature_Data_Buffer[0]) << 8) | (uint16_t)CT75_Temperature_Data_Buffer[1]) >> 4); //ct75  >> 4 lm75 >>5
	return ut;
}

void CT75_TIMEOUT_UserCallback(void)
{
	printf("I2C_Read_NBytes failed\r\n");
}

void CT75_SET_ALERT_LIMIT(uint16_t ct75_addr)
{
	uint8_t reg_val[CT75_TEMPERATURE_NUMBER];
	uint8_t  reg_addr;
 
	reg_addr = 0x03;
	reg_val[0]  = 0x50; //80 degree
	reg_val[1]  = 0x00;
	if(HAL_I2C_Mem_Write(&hi2c2, ct75_addr, reg_addr, I2C_MEMADD_SIZE_8BIT,reg_val,CT75_TEMPERATURE_NUMBER, 1000) != HAL_OK)
	{
		CT75_TIMEOUT_UserCallback();
	}
	//Set the TL limit
	reg_addr = 0x02;
	reg_val[0]  = 0x4B; //75 degree 
	reg_val[1]  = 0x00;
	if(HAL_I2C_Mem_Write(&hi2c2, ct75_addr, reg_addr, I2C_MEMADD_SIZE_8BIT,reg_val,CT75_TEMPERATURE_NUMBER, 1000) != HAL_OK)
	{
		CT75_TIMEOUT_UserCallback();
	}
}

int test_ct75(void)
{
	float TempReadValue[3] = {0};
	float Temperature1;

	TempReadValue[0] = (float)CT75_Read_Temperature(CT75_WRITE_ADDR1) * 0.0625;//0.125;
	Temperature1 = (uint16_t)(TempReadValue[0] * 100);
	printf("Temperature1:%f\r\n", (float)Temperature1 / 100);
	return 0;
}

/****************************************eeprom*******************************************************************************/
int test_eeprom(void)
{
	uint8_t I2C_Buffer_Write[256];
	uint8_t I2C_Buffer_Read[256];
	uint16_t i = 0;
	printf("\r\n \r\n*********STM32CubeMX I2C AT24C02 Example*********\r\n");
	printf("\r\n I2C Write Buffer:\r\n");
	for(i=0; i<256; i++)
	{
		I2C_Buffer_Write[i]=i;    /* WriteBuffer Initialization */
		printf("%02X ", I2C_Buffer_Write[i]);

	}
	for(i=0; i<256; i=i+8)
	{
		if (HAL_I2C_Mem_Write(&hi2c2, ADDR_AT24C02_Write, i, I2C_MEMADD_SIZE_8BIT,&(I2C_Buffer_Write[i]),8, 1000) == HAL_OK)
		{
		  printf("\r\n Byte %02d to Byte %02d Write OK",i,i+8);
		  HAL_Delay(5);//写完以后需要延时5ms，这个不能少
		}
		else
		  printf("\r\n Byte %02d to Byte %02d Write Failed",i,i+8);
	}
	HAL_I2C_Mem_Read(&hi2c2, ADDR_AT24C02_Read, 0, I2C_MEMADD_SIZE_8BIT,I2C_Buffer_Read,256, 1000);
	for(i=0; i<256; i++)
	{
		printf("0x%02X  ",I2C_Buffer_Read[i]);
	}
	return 0;
}

/****************************************主从模式测试******************************************************************************/
/*验证从中断接收,micro 板i2c2发i2c1收*/
uint8_t I2C_slave_send[2] = {4};
uint8_t I2C_send[1] = {1};
uint8_t I2C_Buffer_Slave[TEST_NUM] = {0};
uint8_t I2C_Buffer_master_rsv[TEST_NUM] = {0};
void test_slave_mode(void)
{
	uint8_t i2c_slave_add = I2C2_SLAVE_ADD;
	//printf("\r\n \r\n*********I2C slave Example*********\r\n");
	HAL_I2C_Slave_Receive_IT(&hi2c1/*接收的I2C*/,I2C_Buffer_Slave/*存放数据的地址*/,sizeof(I2C_Buffer_Slave)/*接收数据的个数*/);
	while(1)
	{
		if (HAL_I2C_Mem_Write(&hi2c2, (i2c_slave_add<<1)|0x00, 0x00,I2C_MEMADD_SIZE_8BIT,I2C_send,TEST_NUM, 1000) != HAL_OK)
		{
		  printf("\r\n Byte  Write 1 Failed\n");
		}
		
		HAL_Delay(1000);
	}
	
}

void test_slave_mode_print(void)
{
	int i;
	for(i=0; i<TEST_NUM; i++)
	{
		printf("%d  ",I2C_Buffer_Slave[i]);
		I2C_Buffer_Slave[i] = 0;
	}
	printf("\n");

}
void test_slave_recive(void)
{
	HAL_I2C_Slave_Receive_IT(&hi2c1/*接收的I2C*/,I2C_Buffer_Slave/*存放数据的地址*/,sizeof(I2C_Buffer_Slave)/*接收数据的个数*/);
}


/* 从机接收回调函数 
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	test_slave_mode_print();

	HAL_I2C_Slave_Receive_IT(&hi2c1,I2C_Buffer_Slave,sizeof(I2C_Buffer_Slave));

}
*/

/*********************两板互连验证**********************************/
/*验证i2c主读，从发*/
uint8_t mas_rcv_data[10] = {0};
uint16_t i2c_slave_add = I2C1_SLAVE_ADD;
uint16_t temp_reg =0x5c;

void test_i2c_write(void)
{
	uint8_t send_data[5] = {0x04, 0x80,0,0,0x02};
	uint8_t i2c_slave_add = I2C1_SLAVE_ADD;
	
	if((HAL_I2C_Mem_Write(&hi2c2, (i2c_slave_add<<1)|0x00, temp_reg, I2C_MEMADD_SIZE_8BIT, send_data, 5, 1000)) != HAL_OK)
	{
		printf("mw f\r\n");
	}
}
void test_i2c_read_single(void)
{
	uint8_t Temp[256] = {0x00,0x01,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69};
	uint8_t j = 0;
	
	if((HAL_I2C_Mem_Read(&hi2c2, (i2c_slave_add<<1)|0x01, temp_reg, I2C_MEMADD_SIZE_8BIT, mas_rcv_data, 5, 1000)) == HAL_OK)
	{
		printf("mr:");
		for(j = 0; j<5; j++ )
		{
			printf("%x ",mas_rcv_data[j]);
			mas_rcv_data[j] = 0;
		}
		printf("\r\n");
	}else {
		printf("mr f\r\n");
	}
	
}
void test_i2c_read(void)
{
	uint8_t Temp[256] = {0x00,0x01,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69};
	uint8_t j = 0;
	
	if((HAL_I2C_Mem_Read(&hi2c2, (i2c_slave_add<<1)|0x01, temp_reg, I2C_MEMADD_SIZE_8BIT, mas_rcv_data, 5, 1000)) == HAL_OK)
	{
		printf("mr:");
		for(j = 0; j<5; j++ )
		{
			printf("%x ",mas_rcv_data[j]);
			mas_rcv_data[j] = 0;
		}
		printf("\r\n");
	}else {
		printf("mr f\r\n");
	}
	
}

/*********************从中断读响应**********************************/
uint8_t slave_rcv_reg = 0;
uint8_t slave_rcv_arr[10] = {0};
uint8_t reg_vale = 0x01;//模拟reg表索引值
uint8_t mw_flag = 0;//mem 读写flag
uint8_t mr_flag = 0;//mem 读写flag
uint8_t mr_af_mw_flag = 0;
uint8_t temp_flag = 0;
uint8_t cmd_flag = 0;


void test_bmc_func(void)
{
	  HAL_I2C_EnableListen_IT(&hi2c1);
}

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
	uint8_t Direction = TransferDirection;
	uint8_t Temp[256] = {0x41,0x01,0x01,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69};
	uint8_t ret_sucess[5] = {0x04,00,00,00,0x1F};
	uint8_t ret_temp[5] = {0x04,00,115,00,00};
	uint8_t ret_ready = 0x1;
	
	__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
 

	if(hi2c == &hi2c1)
	{	
		//printf("ad:%d ",Direction);
		if(Direction == 1)//Read 5320
		{
			if(mr_flag == 1)//读写入的寄存器
			{
				if(temp_flag == 1)//寄存器5d
				{//printf("ts t\r\n");
				HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, ret_temp, 5, I2C_FIRST_FRAME);
					temp_flag = 0;
				}else if(cmd_flag)//寄存器5c
				{//printf("ts c\r\n");
					HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, ret_sucess, 5, I2C_FIRST_FRAME);
				}else
				{
					HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, Temp, 5, I2C_FIRST_FRAME);

					//printf("ad:err\r\n");
				}
				mr_flag = 0;//读完成
				mr_af_mw_flag--; //写读完成
			}
			else//直接读
			{
				HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &ret_ready, 1, I2C_FIRST_FRAME);
				//HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &Temp[reg_vale], 1, I2C_FIRST_FRAME);
			}
			//mw_flag = 0;
			
		}
		else
		{
			mw_flag = 1;//写第一个字节
			mr_flag = 1;
			if(mr_af_mw_flag == 0)//块写开始，忽略只读
			{
				
				HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, slave_rcv_arr, 6, I2C_LAST_FRAME);
			} else
			{
				HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_reg, 1, I2C_LAST_FRAME_NO_STOP);

			}
			
		}
	}
}

void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{ 
	//printf("LC");
	if(hi2c == &hi2c1)
	{//printf("1\r\n");
		//if(mr_af_mw_flag)
			
	   HAL_I2C_EnableListen_IT(&hi2c1);
		
			
	  // HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_reg, 1, I2C_FIRST_AND_NEXT_FRAME);
	}
}
/*
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
		printf("reg:%x \r\n",slave_rcv_reg);
		if (slave_rcv_reg == 0x5D)
		temp_flag = 1;
		HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_reg, 1, I2C_FIRST_AND_NEXT_FRAME);


}*/

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	if(mw_flag == 1)//收到的第一个写字节
	{
		mw_flag = 0;
			if(mr_af_mw_flag == 0)//块写开始，忽略只读
			{
		///printf("reg:%x %x %x %x %x %x\r\n",slave_rcv_arr[0],slave_rcv_arr[1],slave_rcv_arr[2],slave_rcv_arr[3],slave_rcv_arr[4],slave_rcv_arr[5]);
				//HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, slave_rcv_arr, 6, I2C_LAST_FRAME);
				//
				mr_af_mw_flag = 2;
			} else
			{
				//HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_reg, 1, I2C_FIRST_AND_NEXT_FRAME);
		///	printf("reg:%x \r\n",slave_rcv_reg);

			}
		temp_flag = 0;
		cmd_flag = 0;
		reg_vale = slave_rcv_reg;
		if (slave_rcv_reg > 0x61)
		{
			reg_vale = slave_rcv_reg - 0x62 + 2;
		} else if(slave_rcv_reg == 0x5c)
		{
			cmd_flag = 1;
			reg_vale = 2;
		}
		else if(slave_rcv_reg == 0x5d)
		{
			temp_flag = 1;
			reg_vale = 2;
		}

	}
	else
	{
		printf("arr:%x",slave_rcv_arr[0]);
		printf(" %x",slave_rcv_arr[1]);
		printf(" %x",slave_rcv_arr[2]);
		printf(" %x",slave_rcv_arr[3]);
		printf(" %x\n\r",slave_rcv_arr[4]);
		mr_af_mw_flag = 1;
	}
	
	/////HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_reg, 1, I2C_FIRST_AND_NEXT_FRAME);

}
